<?php
/**
 * 装修公司
 */
namespace App\Http\ControllersAdmin;

use App\Service\AdsService;
use App\Validation\AdsValidation;
use App\Validation\AdsSearchValidation;
use App\Validation\DeleteValidation;

class AdsController extends BaseController
{
	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new AdsSearchValidation, $this->request->getJsonRawBody());
        $this->backgroudDateFilterByCityRole($raw);
		$data = AdsService::getSearch($raw);
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			// $d[$key]->pics = json_decode($value->pics);
		}
		$count = AdsService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [createAction 创建]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$this->getUser();
		$raw = $this->validation(new AdsValidation, $this->request->getJsonRawBody());
		// $raw->pics = json_encode($raw->pics);
		$data = AdsService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
        $raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
        $data = AdsService::getModel($raw->id);
		// $data->pics = json_decode($data->pics);
        if($data){

            $data->image_withurl=json_decode($data->image_withurl,true);
        }

        return $this->echoJson($data);
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$this->getUser();
		$raw = $this->validation(new AdsValidation, $this->request->getJsonRawBody());
		// $raw->pics = json_encode($raw->pics);
		$data = AdsService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [upAction 上架]
	 * @return [type] [description]
	 */
	public function displayAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = AdsService::display($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = AdsService::delete($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [foreverAction 恢复]
	 * @return [type] [description]
	 */
	public function foreverAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = AdsService::forever($raw->id);
		return $this->echoJson('ok');
	}

    /**
     * [saveadvAction 恢复]
     * @return [type] [description]
     */
    public function saveadvAction()
    {
        $user=$this->getUser();
        $raw = $this->validation(new AdsValidation, $this->request->getJsonRawBody());
        $raw->image_withurl = json_encode($raw->image_withurl);
        //判断该广告是否在指定时间段添加

        if(isset($user->roles_info) && isset($user->roles_info->city_id)){
            $raw->city_id=$user->roles_info->city_id;
        }

        $advexist=$this->checkAdvExist($raw);

        if($advexist && count($advexist)>0){//有同时段广告存在

            if(isset($raw->id) && $raw->id){//已经存在进行编辑
                if($raw->id!=$advexist[0]['id']){
                    return $this->echoJson('该时段已存在广告，请选择其他时段');
                }
            }else{
                return $this->echoJson('该时段已存在广告，请选择其他时段');
            }

        }


        $data = AdsService::save($raw);
        //print_r($raw);
        //$para= $this->request->getJsonRawBody();
        //AdsService::save($raw);

        //array('pos_id'=>$para['pos_id'],'over_time'=>$para['over_time'],'time_start'=>$para['time_start'],'time_end'=>$para['time_end'],
        //    'date_start'=>$para['date_start'],'title'=>$para['title'],'image_withurl'=>$para['image_withurl']);
        if($data){
            return $this->echoJson('ok');
        }else{
            return $this->echoJson('操作失败');
        }

    }
    /**
     * [checkadvexistAction 恢复]
     * @return [type] [description] 检测选择的时间段是否已经存在广告位
     */
    public function checkadvexistAction(){
        $this->getUser();
        $raw = $this->validation(new AdsValidation, $this->request->getJsonRawBody());
        $d=$this->checkAdvExist($raw);
        //$d=AdsService::checkExistAdvBeTiem(array('type'=>$raw->pos_id,'date_start'=>$raw->date_start,'time_start'=>$raw->time_start,'time_end'=>$raw->time_end,'over_time'=>$raw->over_time));
        return $this->echoJson($d);
    }
    /*检测广告是否已经存在*/
    protected function checkAdvExist($raw){
        $city_id="0";
        if(isset($raw->city_id) && $raw->city_id!=''){
            $city_id=$raw->city_id."";
        }
        $d=AdsService::checkExistAdvBeTiem(array('type'=>$raw->pos_id,'date_start'=>$raw->date_start,'time_start'=>$raw->time_start,'time_end'=>$raw->time_end,'over_time'=>$raw->over_time,'city_id'=>$city_id));
        return $d;
    }
}